<?php
//Test si l'utilisateur est connecté
if ((empty($_SESSION["connect"])||($_SESSION["connect"] != 1))) {
	header("Location: ?pageid=1");
}
/*Importance*/
$importance = getImportance($connmysql)->fetchAll();
/*Categorie de l'utilisateur*/
$categorie   = "";
$categorieJS = "";
$cat = getCategories($connmysql,$_SESSION["iduser"])->fetchAll();
foreach($cat as $c)
{
	$categorie[$c['categoryname']]=$c['idcategory'];
	$categorieJS[]='"'.$c['categoryname'].'"';
}
if(isset($_POST['subform']))
{
	if(empty($_POST['nom']))$errors[]="Le nom est un champs obligatoire";
	if(!empty($_POST['dateDebut'])&&!preg_match("#^[0-9]{2}/[0-9]{2}/[0-9]{4} [0-9]{2}:[0-9]{2}$#",$_POST['dateDebut']))$errors[]="La date de début n'est pas dans un format valide (jj/mm/yyyy HH:MM)";
	if(!empty($_POST['dateFin'])&&!preg_match("#^[0-9]{2}/[0-9]{2}/[0-9]{4} [0-9]{2}:[0-9]{2}$#",$_POST['dateFin']))$errors[]="La date de fin n'est pas dans un format valide (jj/mm/yyyy HH:MM)";
	
	if(!isset($errors))
	{/*Aucune erreur, on sauvegarde !!!!*/
		
		if(!isset($categorie[$_POST['categorie']]))
		{
			addCategory($connmysql,$_POST['categorie'],$_SESSION["iduser"]);
			$cat=$connmysql->lastInsertId();
		}
		else
			$cat=$categorie[$_POST['categorie']];
			
		if(isset($_GET['taskid'])&&is_numeric($_GET['taskid']))
			$result = updateTask($connmysql, $_GET['taskid'], $cat, $_SESSION["iduser"], $_POST['nom'], $_POST['description'], dateToMysql($_POST['dateDebut']), dateToMysql($_POST['dateFin']),$_POST['importance']);
		else
			$result = addTask($connmysql, $_SESSION["iduser"], $_POST['nom'], $_POST['description'], $cat, dateToMysql($_POST['dateDebut']), dateToMysql($_POST['dateFin']),$_POST['importance']);
	}
	
	$taskData['name']=$_POST['nom'];
	$taskData['start']=$_POST['dateDebut'];
	$taskData['end']=$_POST['dateFin'];
	$taskData['description']=$_POST['description'];
	$taskData['categorie']=$_POST['categorie'];
	$taskData['importance']=$_POST['importance'];
}else if(isset($_GET['taskid'])&&is_numeric($_GET['taskid']))
{
	$task = getTask($connmysql, $_GET['taskid'],$_SESSION["iduser"])->fetch();
	if(is_array($task))
	{
		$taskData['name']=$task['taskname'];
		$taskData['start']=dateFromMysql($task['taskstart'],true);
		$taskData['end']=dateFromMysql($task['taskend'],true);
		$taskData['description']=$task['taskdescription'];
		$taskData['categorie']=$task['categoryname'];
		$taskData['importance']=$task['id_importance'];
	}
}
?>
<div id="logform">
	<?php
		if (!empty($result)) {
	?>
	<h2><span style="color:#45A747" ><?php echo $result; ?> </span></h2>
	<?php
		}
	?>
    <form name="subform" method="POST" action="?pageid=5<?php echo (isset($_GET['taskid'])&&is_numeric($_GET['taskid']))?"&taskid=".$_GET['taskid']:"" ?>">
    <fieldset>
    <legend>Ajout / Edition de tâche</legend>
	<?php
	if(isset($errors))
	{
		echo "<ul>";
		foreach($errors as $e)
			echo "<li>".$e."</li>";
		echo "</ul>";
	}
	?>
	<p>Nom *<br>
      <input class="textin" name="nom" placeholder="Nom" value="<?php echo @$taskData['name'];?>" type="text" />
	</p>
    <p>Description<br>
      <input class="textin" name="description" placeholder="Description" value="<?php echo @$taskData['description'];?>" onClick="this.value='';" type="text" />
    </p>
	<div id="accordion">
	  <h3><a href="#">Détails</a></h3>
			<div id="showdetails">
				<p>Importance<br>
				  <select class="textin" name="importance" type="text">
					<?php
					foreach($importance as $i)		
						echo "<option value='".$i[0]."'".(($i[0]==@$taskData['importance'])?" selected='selected'":"").">".$i[1]."</option>";
					?>
				  </select>
				</p>
				<p>Catégorie<br>
				  <input id="cat" class="textin" name="categorie" placeholder="Catégorie" value="<?php echo @$taskData['categorie'];?>" onClick="this.value='';" type="text" />
				</p>
				<p>Date de début<br>
				  <input class="textin datepicker" name="dateDebut"  placeholder="Date de début" value="<?php echo @$taskData['start'];?>" type="text" />
				</p>
				<p>Date de fin<br>
				  <input class="textin datepicker" name="dateFin"  placeholder="Date de fin" value="<?php echo @$taskData['end'];?>" type="text" />
				</p>
			</div>
	</div>
    <p>
      <input type="submit" class="subform" name="subform" value="Enregistrer" />
    </p>
    <p>
    <a class="links" href="?pageid=2">&lt; Retour</a>
    </p>
    </fieldset>
    </form>
	<script type="text/javascript">
	$(function() {
		var availableTags = [<?php echo join(",",$categorieJS);?>];
		$( "#cat" ).autocomplete({source: availableTags,minLength: 0});
		$( ".datepicker" ).datetimepicker({ dateFormat: 'dd/mm/yy',addSliderAccess: true,sliderAccessArgs: { touchonly: false }});

		$( "#accordion" ).accordion({
			collapsible:true,
			autoHeight: false, 
			active: false 
		});
	});
	</script>
</div>